﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameServer.Managers;
using GameServer.Networking.Packets;

namespace GameServer.Networking.Handlers
{
    class HANDLE_CHAT : PacketHandler
    {
        public override void Handle(GameServer.Virtual_Objects.User.virtualUser User)
        {
            try
            {
                //TODO: Add Whisper
                if (RankManager.HasPermision(User.Rank, "account.chat") == false || User.isMuted)
                {
                    User.send(new PACKET_CHAT("SYSTEM", PACKET_CHAT.ChatType.Whisper, "SYSTEM >> You're muted, please try it again", 12, "SYSTEM"));
                    return;
                }

                int ChatType = int.Parse(getBlock(0));
                int TargetID = int.Parse(getBlock(1));

                string TargetName = getBlock(2);
                string Message = getBlock(3);

                string sMessage = Message.Split(new string[] { ">>" + Convert.ToChar(0x1D).ToString() }, StringSplitOptions.None)[1].Replace(Convert.ToChar(0x1D), Convert.ToChar(0x20));
                if (User.isCommand(sMessage)) return;
                //DB.runQuery("INSERT INTO chatlogs (`userid`, `roomid`, `message`, `timestamp`) VALUES ('" + User.UserID + "', '" + ((User.Room == null) ? -1 : User.Room.ID) + "', '" + DB.Stripslash(sMessage) + "', '" + Program.currTimeStamp + "')");

                switch (ChatType)
                {
                    case 2:
                        {
                            Virtual_Objects.User.virtualUser Target = UserManager.getUser(TargetName);
                            if (Target is Virtual_Objects.User.virtualUser && Target.SessionID > 0)
                            {
                                User.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Whisper, Message, Target.SessionID, Target.Nickname));
                                if (!User.Equals(Target))
                                {
                                    Target.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Whisper, Message, Target.SessionID, Target.Nickname));
                                }
                            }
                            else
                            {
                                User.send(new PACKET_CHAT(PACKET_CHAT.ErrorCodes.ErrorUser, TargetName + Convert.ToChar(0x1D)));
                            }
                            break;
                        }
                    case 3: // Lobby2Channel
                        {
                            UserManager.sendToChannel(User.Channel, false, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                            break;
                        }
                    case 4: // Room2All
                        {
                            if (User.Room != null)
                            {
                                if (User.hasItem("CC02") && User.Room.RoomStatus == 1 && User.RoomSlot == User.Room.RoomMasterSlot && User.Rank < 3)
                                    TargetID = 998;
                                else if (User.Rank > 2)
                                    TargetID = 999;

                                User.Room.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Room_ToAll, Message, TargetID, TargetName));
                            }
                            else
                            {
                                User.disconnect();
                            }
                            break;
                        }
                    case 5: // Room2Team
                        {
                            if (User.Room != null)
                            {
                                foreach (Virtual_Objects.User.virtualUser RoomUser in User.Room.Players)
                                {
                                    if (User.Room.getSide(User) == User.Room.getSide(RoomUser))
                                    {
                                        RoomUser.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Room_ToTeam, Message, (User.Rank > 2) ? 999 : TargetID, TargetName));
                                    }
                                }
                                foreach (Virtual_Objects.User.virtualUser SpectatorUser in User.Room.Spectators)
                                {
                                    string Side = (User.Room.getSide(User) == 0 ? "[DERB]" : "[NIU]");
                                    SpectatorUser.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Room_ToTeam, Side + Message, (User.Rank > 2) ? 999 : TargetID, TargetName));
                                }
                            }
                            else
                            {
                                User.disconnect();
                            }
                            break;
                        }
                    case 6: // Whisper
                        {
                            Virtual_Objects.User.virtualUser Target = UserManager.getUser(TargetName);
                            if (Target is Virtual_Objects.User.virtualUser && Target.SessionID > 0 && Target.GMMode == false)
                            {
                                User.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Whisper, Message, Target.SessionID, Target.Nickname));
                                if (!User.Equals(Target))
                                {
                                    Target.send(new PACKET_CHAT(User, PACKET_CHAT.ChatType.Whisper, Message, Target.SessionID, Target.Nickname));
                                }
                            }
                            else
                            {
                                User.send(new PACKET_CHAT(PACKET_CHAT.ErrorCodes.ErrorUser, TargetName + Convert.ToChar(0x1D)));
                            }
                            break;
                        }
                    case 8: //Lobby2AllChannels
                        {
                            UserManager.sendToChannel(1, false, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                            UserManager.sendToChannel(2, false, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                            UserManager.sendToChannel(3, false, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                            UserManager.sendToChannel(4, false, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Lobby_ToChannel, Message, TargetID, TargetName));
                            break;
                        }
                   case 10: // Clan
                        {
                            if(User.ClanID != -1)
                                ClanManager.sendToClan(User, new PACKET_CHAT(User, PACKET_CHAT.ChatType.Clan, Message, TargetID, TargetName));
                            else
                                User.send(new PACKET_CHAT("SYSTEM", PACKET_CHAT.ChatType.Whisper, "SYSTEM >> Available after creating a clan", User.SessionID, User.Nickname));
                            break;
                        }
                }
                string[] args = sMessage.Split(Convert.ToChar(0x20));
                if (args.Length >= 2)
                {
                    if (args[0] == "WarRock" && args[1] == "EngineWar" && args[2] == "Forever!")
                    {
                        if (User.CheckForEvent(5) == false)
                        {
                            //User.send(new PACKET_CHAT_EVENT(User, "DS01"));
                            User.send(new PACKET_CHAT("SYSTEM", PACKET_CHAT.ChatType.Whisper, "SYSTEM >> You got 1,000 cash for chat event :)", User.SessionID, User.Nickname));
                            DB.runQuery("INSERT INTO users_events (eventid, userid) VALUES ('5','" + User.UserID + "')");
                            User.Cash += 1000;
                            DB.runQuery("UPDATE users SET cash=cash+1000 WHERE id='" + User.UserID + "'");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
            }
        }
    }
}
